package ssm.controller;


import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import ssm.dao.*;
import ssm.service.*;

import java.text.ParseException;
import java.util.*;

@Controller
@RequestMapping("/ykt")
public class yktController {

    String reg = "/^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$/";


    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private YktCarinfoService yktCarinfoService;

    @Autowired
    private TrackService trackService;

    @Autowired
    private TrackCarService trackCarService;

    @Autowired
    private FollowService followService;


    //1.OK-有些小问题
    @RequestMapping("/getPersonListTime")
    @ResponseBody
    public MyResult getPersonListTime(@RequestParam String starttime,
                                      @RequestParam String endtime,
                                      @RequestParam int page,
                                      @RequestParam int pageNum) {
        if (starttime == null || starttime.equals("")) {
            return MyResult.except("时间缺失", "查询起始时间不能为空");
        }
        if (endtime == null || endtime.equals("")) {
            return MyResult.except("时间缺失", "查询终止时间不能为空");
        }
        Map<String, Object> result = trackService.getByTime(starttime, endtime, page, pageNum);
        return MyResult.ok(result);
    }

    //2.OK-已修改返回格式
    @RequestMapping(value = "/getCarListTime")
    @ResponseBody
    public MyResult getCarListTime(@RequestParam String starttime,
                                   @RequestParam String endtime,
                                   @RequestParam(value = "cph", required = false) String cph,
                                   @RequestParam(value = "page", required = false) Integer page,
                                   @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        if (starttime == null || starttime.equals("")) {
            return MyResult.except("时间缺失", "查询起始时间不能为空");
        }
        if (endtime == null || endtime.equals("")) {
            return MyResult.except("时间缺失", "查询终止时间不能为空");
        }

        Map<String,Object>param=new HashMap<String,Object>();
        param.put("starttime",starttime);
        param.put("endtime",endtime);
        if(cph==null&&(page==null&&pageNum==null)){
            return MyResult.except("传参错误", "jid跟分页参数必须传入其中一组参数");
        }

        if(cph!=null){
            param.put("cph",cph);
        }
        if(page!=null&&pageNum!=null){
            int pageStart=(page)*pageNum;
            param.put("pageStart", pageStart);
            param.put("pageEnd", pageNum);

        }
        Map<String, Object> result = trackCarService.findByMap(param);
        return MyResult.ok(result);
    }

    //3.OK--格式尚未确定
    @RequestMapping("/getStudentInfo")
    @ResponseBody
    public MyResult getStudentInfo(@RequestParam String xh) {
        if (xh == null && xh.equals("")) {
            return MyResult.except("学号为空", "请输入学号");
        }
        Student student = studentService.getByXh(xh);
        return MyResult.ok(student);
    }

    //4.OK--格式尚未确定
    @RequestMapping("/getTeacherInfo")
    @ResponseBody
    public MyResult getTeacherInfo(@RequestParam String gh) {
        if (gh == null && gh.equals("")) {
            return MyResult.except("教师工号为空", "请输入教师工号");
        }
        Teacher teacher = teacherService.getByGh(gh);
        return MyResult.ok(teacher);
    }


    //5.OK---待定
    @RequestMapping("/getCarInfo")
    @ResponseBody
    public MyResult getCarInfo(
                               @RequestParam(value = "jid", required = false) String jid) {
        List<TrackCar> trackCar = yktCarinfoService.getByCphOrGh(jid);
        return MyResult.ok(trackCar);
    }


    //6.ok-已修改返回格式
    @RequestMapping("/getPerTrack")
    @ResponseBody
    public MyResult getPerTrack(@RequestParam String starttime,
                                @RequestParam String endtime,
                                @RequestParam(value = "jid", required = false) String jid,
                                @RequestParam(value = "page", required = true) Integer page,
                                @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                @RequestParam(value = "list",required = false)List<String> list) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("starttime", starttime);
        map.put("endtime", endtime);
        if (jid == null && (page == null || pageNum == null)) {
            return MyResult.except("传参错误", "jid跟分页参数必须传入其中一组参数");
        }

        if (jid != null && jid != "") {
            map.put("jid", jid);
        }

        if (page != null && pageNum != null) {
            int pageStart=(page)*pageNum;
            map.put("pageStart", pageStart);
            map.put("pageEnd", pageNum);
        }
        Set<String> lys=new HashSet<String>();
       if(list!=null){
           for(String str:list) {
               if ("internet".equals(str)) {
                   lys.add("wifi_use");
                   lys.add("internet_use");
               } else if ("ykt".equals(str)) {
                   lys.add("ykt_library");
                   lys.add("ykt_locker_use");
                   lys.add("ykt_entguard");
                   lys.add("ykt_business");
               } else if ("car".equals(str)) {
                   lys.add("track_car");
               } else if ("face".equals(str)) {
                   lys.add("ykt_face");
               }
           }
        }

        if(lys!=null&&!lys.isEmpty()&&lys.size()>0){
            map.put("list",lys);
        }
        Map<String, Object> tracks = trackService.getByTimeOrJid(map);
        return MyResult.ok(tracks);


    }

    //7.OK--格式已修改完毕
    @RequestMapping("/getCarTrack")
    @ResponseBody
    public MyResult getCarTrack(@RequestParam String starttime,
                                @RequestParam String endtime,
                                @RequestParam String cph,
                                @RequestParam int page,
                                @RequestParam int pageNum) {
        if (starttime == null || starttime.equals("")) {
            return MyResult.except("时间缺失", "查询起始时间不能为空");
        }
        if (endtime == null || endtime.equals("")) {
            return MyResult.except("时间缺失", "查询终止时间不能为空");
        }
        Map<String,Object> result = trackCarService.getOneByTime(cph, starttime, endtime,page,pageNum);
        return MyResult.ok(result);
    }

   /* //8.OK--传回原始数据
    @RequestMapping("/getCarAction")
    //getBayonet接口
    @ResponseBody
    public MyResult getCarAction(
                                 @RequestParam String cph,
                                 @RequestParam String starttime,
                                 @RequestParam String endtime){
        if(starttime==null||starttime.equals("")){
            return MyResult.except("时间缺失","查询起始时间不能为空");
        }
        if(endtime==null||endtime.equals("")){
            return MyResult.except("时间缺失","查询终止时间不能为空");
        }
        List<Map<String,Object>>cars=trackCarService.getByMap(cph,starttime,endtime);
        return MyResult.ok(cars);


        *//*List<TestActionCar> list=new ArrayList<TestActionCar>();
        TestActionCar car = new TestActionCar();
        car.setId(1);
        car.setCarNum("浙ASJ867");
        car.setBayonetName("天竺路东");
        car.setElapsedTime("2017-10-23 13:27:17");
        car.setLicense("92式民用车");
        car.setVehicleType("大型汽车");
        car.setVehicleBrand("福特");
        //car.setHomeLocation("浙江省杭州市");
        list.add(car);
        TestActionCar car1 = new TestActionCar();
        car1.setId(2);
        car1.setCarNum("浙ASB267");
        car1.setBayonetName("校门口球机");
        car1.setElapsedTime("2017-10-23 13:32:17");
        car1.setLicense("92式民用车");
        car1.setVehicleType("中型汽车");
        car1.setVehicleBrand("宝马");
        car1.setHomeLocation("浙江省杭州市");
        list.add(car1);
        TestActionCar car2 = new TestActionCar();
        car2.setId(3);
        car2.setCarNum("浙ARJ725");
        car2.setBayonetName("车库1");
        car2.setElapsedTime("2017-10-23 14:01:52");
        car2.setLicense("92式民用车");
        car2.setVehicleType("大型汽车");
        car2.setVehicleBrand("保时捷");
        car2.setHomeLocation("浙江省杭州市");
        list.add(car2);
        TestActionCar car3 = new TestActionCar();
        car3.setId(4);
        car3.setCarNum("浙AWR872");
        car3.setBayonetName("东围墙");
        car3.setElapsedTime("2017-10-23 13:33:24");
        car3.setLicense("92式民用车");
        car3.setVehicleType("小型汽车");
        car3.setVehicleBrand("福特");
        car3.setHomeLocation("浙江省杭州市");
        list.add(car3);
        TestActionCar car4 = new TestActionCar();
        car4.setId(5);
        car4.setCarNum("浙AJH067");
        car4.setBayonetName("天竺路东");
        car4.setElapsedTime("2017-10-23 13:34:29");
        car4.setLicense("92式民用车");
        car4.setVehicleType("大型汽车");
        car4.setVehicleBrand("宝马");
        car4.setHomeLocation("浙江省杭州市");
        list.add(car4);*//*
        //return MyResult.ok(list);
    }*/

    //先跳过,可能删除
    @RequestMapping("/getCardUse")
    @ResponseBody
    public MyResult getCardUse(
            @RequestParam(value = "kh", required = false) String kh,
            @RequestParam String starttime,
            @RequestParam String endtime) {
        if (starttime == null || starttime.equals("")) {
            return MyResult.except("时间缺失", "查询起始时间不能为空");
        }
        if (endtime == null || endtime.equals("")) {
            return MyResult.except("时间缺失", "查询终止时间不能为空");
        }
        //String reg="/^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$/";
       /* if(!starttime.matches(reg)||!endtime.matches(reg)){
            return MyResult.except("时间格式错误","请审查输入时间格式,例:2017-11-11 00:00:00");
        }*/
        try {//需要遍历ids,查看设备分布id
            //根据cp去查询
            //执行sql语句出错
            //throw new Exception();
        } catch (Exception e) {
            return MyResult.except("时间格式错误", "请审查输入时间格式,例:2017-11-11 00:00:00");
        }

        List<TestMJ> list = new ArrayList<TestMJ>();
        TestMJ mj = new TestMJ();
        mj.setId(1);
        mj.setName("王晓明123");
        mj.setTime("2017-10-23 13:27:17");
        mj.setPlace("18号宿舍楼");
        mj.setAction("门禁");
        list.add(mj);
        TestMJ mj1 = new TestMJ();
        mj1.setId(2);
        mj1.setName("王大陆123");
        mj1.setTime("2017-10-23 13:27:58");
        mj1.setPlace("图书馆");
        mj1.setAction("借书");
        list.add(mj1);
        TestMJ mj2 = new TestMJ();
        mj2.setId(3);
        mj2.setName("王宝强123");
        mj2.setTime("2017-10-23 13:28:05");
        mj2.setPlace("1号食堂楼下");
        mj2.setAction("消费");
        list.add(mj2);
        TestMJ mj3 = new TestMJ();
        mj3.setId(4);
        mj3.setName("李树杰");
        mj3.setTime("2017-10-23 13:29:01");
        mj3.setPlace("实验室");
        mj3.setAction("门禁");
        list.add(mj3);
        TestMJ mj4 = new TestMJ();
        mj4.setId(5);
        mj4.setName("李白");
        mj4.setTime("2017-10-23 13:29:50");
        mj4.setPlace("游泳馆");
        mj4.setAction("消费");
        list.add(mj4);
        return MyResult.ok(list);
    }

    //假接口,表尚未确定
    @RequestMapping("/getPersonImg")
    @ResponseBody
    public MyResult getPersonImg(@RequestParam String jid) {
        if (jid == null && jid.equals("")) {
            return MyResult.except("参数缺失", "请输入工号/学号");
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("imgurl", "../img/person/person1-origin.png");
        map.put("imgurlnew", "../img/person/person1-new.png");
        map.put("simacc", "85.01%");
        return MyResult.ok(map);
    }


    @RequestMapping("/getCarfellowperson")
    @ResponseBody
    public MyResult getCarfellowcar(@RequestParam String starttime,
                                    @RequestParam String endtime,
                                    @RequestParam String jid) {

        return MyResult.ok();
    }

    @RequestMapping("/getCarfellowcar")
    @ResponseBody
    public MyResult getPerfellowcar(@RequestParam String starttime,
                                    @RequestParam String endtime,
                                    @RequestParam String cph) {
        /*List<Map>list=new ArrayList<Map>();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("carPlate","浙E89123");
        map.put("plateColor","蓝色");
        map.put("matchCount","20");
        map.put("matchrate","高");
        list.add(map);
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("carPlate","浙E89124");
        map1.put("matchrate","中");
        map1.put("plateColor","黄色");
        map1.put("matchCount","20");
        list.add(map1);*/


        List<Map> list = null;
        try {
            list = followService.carFellowCar(cph, starttime, endtime);
        } catch (ParseException e) {
            return MyResult.except("格式转化错误", "请检查时间格式'yyyy-MM-dd HH:mm:ss'");
        }
        return MyResult.ok(list);

    }


    @RequestMapping("/getPerfellowcar")
    @ResponseBody
    public MyResult getCarfellowperson(@RequestParam String starttime,
                                       @RequestParam String endtime,
                                       @RequestParam String cph) {


        return MyResult.ok();
    }

    @RequestMapping("/getPerfellowperson")
    @ResponseBody
    public MyResult getPerfellowperson(@RequestParam String starttime,
                                       @RequestParam String endtime,
                                       @RequestParam String jid) {
        String date=starttime.substring(0,10);
        System.out.println(date);

        try {
            List<Map<String,Object>>result = followService.perFellowPerson(jid, starttime,endtime);
            return MyResult.ok(result);
        } catch (ParseException e) {
            return MyResult.except("格式转化错误", "请检查时间格式'yyyy-MM-dd HH:mm:ss'");
        }
        //List<Map>list=new ArrayList<Map>();
        /*HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("xm","张三");
        map.put("jid","666666");
        map.put("jysj","2017-11-16 12:00:00");
        map.put("sh","食堂一楼(牛肉面房)");
        map.put("shmc","食堂一楼(牛肉面房)");
        map.put("jye","-1.8");
        map.put("lsh","91288883");
        map.put("ye","28.6");
        map.put("shlxmc","消费");
        list.add(map);
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("xm","张三");
        map1.put("jid","666666");
        map1.put("jysj","2017-11-16 12:01:21");
        map1.put("sh","宿舍热水系统");
        map1.put("shmc","宿舍热水系统");
        map1.put("jye","-1.2");
        map1.put("lsh","91288884");
        map1.put("ye","27.4");
        map1.put("shlxmc","热水");
        list.add(map1);*/

    }


    @RequestMapping("/gexfjl")
    @ResponseBody
    public MyResult gexfjl(@RequestParam String starttime,
                           @RequestParam String endtime,
                           @RequestParam String jid) {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("starttime",starttime);
        param.put("endtime",endtime);
        param.put("jid",jid);
        //todo 接口修正
        List<YktBusiness> result=trackService.getBusinessDetail(param);
        return MyResult.ok(result);
    }


}



